package com.twm.redis;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


@Component
@Data
@ConfigurationProperties(prefix="redis")
public class RedisPropertiesConfig {
	@Value("${redis.host}")
	private String host;

	private int port;

	private int timeout;//秒

	private String password;

	private int poolMaxTotal;

	private int poolMaxIdle;

	private int poolMaxWait;//秒

	private int database = 0;


	@Bean
	public JedisPool redisPoolFactory() {
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxIdle(this.getPoolMaxIdle());
		poolConfig.setMaxTotal(this.getPoolMaxTotal());

		poolConfig.setMaxWaitMillis(this.getPoolMaxWait() * 1000);
		JedisPool jedisPool = new JedisPool(poolConfig, this.getHost(), this.getPort(),
				this.getTimeout() * 1000, this.getPassword(), database);

		return jedisPool;
	}

	@Override
	public String toString() {
		return "RedisConfig{" +
				"host='" + host + '\'' +
				", port=" + port +
				", timeout=" + timeout +
				", password='" + password + '\'' +
				", poolMaxTotal=" + poolMaxTotal +
				", poolMaxIdle=" + poolMaxIdle +
				", poolMaxWait=" + poolMaxWait +
				'}';
	}

}
